package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @aothor LentDent1
 * @vision 1.0
 */

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关业务")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐业务
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐业务")
    @CacheEvict(cacheNames = "etmealCache", key = "#setmealDTO.categoryId")
    public Result save (@RequestBody SetmealDTO setmealDTO) {

        // 调用service层
        setMealService.saveWithDish(setmealDTO);

        return Result.success();

    }
    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page (SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true) // 删除所有的缓存
    public Result delete (@RequestParam List<Long> ids) {
        // 调用service层
        setMealService.delete(ids);

        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectById (@PathVariable Long id) {

        // 调用service方法
        SetmealVO setmealVO = setMealService.selectById(id);

        return Result.success(setmealVO);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update (@RequestBody SetmealDTO setmealDTO) {

        // 调用service层方法
        setMealService.update(setmealDTO);

        return Result.success();
    }

    /**
     * 修改套餐销售状态
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐销售状态")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true) // 删除套餐所有缓存
    public Result updateStatus (@PathVariable Integer status, Long id) {
        
         setMealService.updateStatus (status,id) ;
        
        return Result.success();
    }
}
